package persistencia;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import dominio.*;

public class AutorDAO {

	public static Autor newAutor(String nif) throws Exception {
		Autor result=null;
		result = new Autor();
		String SQL="Select * from Persona,Autor where Persona.dni=? and Persona.dni=Autor.dni";
		Conexion bd=null;
		Exception ex=null;
		PreparedStatement p=null;
		try {
				bd = Broker.get().getBD();
				p = bd.prepareStatement(SQL);
				p.setString(1, nif);
				ResultSet r = p.executeQuery();
				if (r.next()) {
					result.setDni(r.getString("dni"));
					result.setNombre(r.getString("nombre"));
					result.setApellido(r.getString("apellido"));
					result.setAfiliacion(r.getString("afiliacion"));
					result.setEmail(r.getString("email"));
					result.setRol(r.getString("rol"));
					result.setPassword(r.getString("pass"));
					result.setIdAutor(r.getInt("idAutor"));
				} else {
					throw new Exception("La persona con NIF " + nif + " no existe");
				}			
		} catch (Exception e) {
			ex = e;
	}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}
		return result;
	}
	
	
	public static void update (Autor aut) throws Exception{
		PersonaDAO.update(aut);
	}
	
	

	public static void insert(Autor autor) throws Exception{
		PersonaDAO.insert(autor);
		String SQL="Insert into Autor (dni) values (?)";
		Conexion bd=null;
		Exception ex=null;
		PreparedStatement p=null;
		try {
			bd=Broker.get().getBD();
			p=bd.prepareStatement(SQL);
			p.setString(1, autor.getDni());
			p.executeUpdate();
		}
		catch (Exception e) {
			ex=e;
		}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}
	}
	
	public static void delete(Autor autor) throws Exception {
		PersonaDAO.delete(autor);
	}
	
	/*public static Vector<Autor> cargaSimbolica() throws Exception{
		Vector<Autor> ret = new Vector<Autor>();
		return ret;
	}*/
	
	public static Vector<Autor> getAutores() throws Exception {
		Vector<Autor> result=new Vector<Autor>();
		String SQL="Select (dni) from Persona where rol='Autor'";
		Conexion bd=null;
		Exception ex=null;
		PreparedStatement p=null;
		try {
			bd=Broker.get().getBD();
			p=bd.prepareStatement(SQL);
			ResultSet r=p.executeQuery();
			while (r.next()) {
				Autor autor = newAutor(r.getString(1));
				result.add(autor);
			}			
		}
		catch (Exception e) {
			ex=e;
		}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}
		return result;
	}
	
	public static void castRevisorToAutor(Persona persona) throws Exception{
		persona.setRol("Autor");
		PersonaDAO.update(persona);
		
		String SQL="Delete from Revisor where dni=?";
		Conexion bd=null;
		Exception ex=null;
		PreparedStatement p=null;
		try {
			bd=Broker.get().getBD();
			p=bd.prepareStatement(SQL);
			p.setString(1, persona.getDni());
			p.executeUpdate();
		}
		catch (Exception e) {
			ex=e;
		}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}
		
		SQL="Select * from Autor where dni=?";
		
		try {
			p=bd.prepareStatement(SQL);
			p.setString(1, persona.getDni());
			ResultSet r=p.executeQuery();
			if (!r.next()) {
				SQL="Insert into Autor (dni) values (?)";
				p=bd.prepareStatement(SQL);
				p.setString(1, persona.getDni());
				p.executeUpdate();
			}			
		}
		catch (Exception e) {
			ex=e;
		}
		finally {
			bd.close();
			if (ex!=null)
				throw ex;
		}
	}
	
	
}
